package com.sale.succ.datacomp.region;

import static com.sale.succ.utils.TraderHelper.getLabel;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.sale.succ.components.MessDialog;
import com.sale.succ.objects.Session;
import com.sale.succ.system.txt.ObjectsProvider;
import com.sale.succ.utils.TraderConstants;

import eve.fx.Color;
import eve.ui.table.TableCellAttributes;
import eve.ui.table.TableModel;

public class RegionTableModel extends TableModel {

	private MessDialog loadingDialog;
	private HashMap clientGroups;
	private HashMap row2Id;

	public RegionTableModel() {
		loadingDialog = new MessDialog(getLabel(TraderConstants.LOADING),
				MessDialog.NOB);
		loadingDialog.exec();
		try {
			initData();
		} finally {
			loadingDialog.close(0);
		}

		numRows = clientGroups.size();
		numCols = 1;
		hasRowHeaders = false;
		hasColumnHeaders = false;
		fillToEqualWidths = true;

	}

	private void initData() {
		row2Id = new HashMap();

		clientGroups = ObjectsProvider.getInstance().getGroupCollection();
		Set ids = clientGroups.keySet();
		Iterator it = ids.iterator();
		int i = 0;
		while (it.hasNext()) {
			row2Id.put(String.valueOf(i), it.next().toString());
			++i;
		}
	}

	public int itemCount() {
		return clientGroups.size();
	}

	public int calculateColWidth(int col) {
		if (col == -1)
			return 0;
		return super.calculateColWidth(col);
	}

	public int calculateRowHeight(int row) {
		if (row == -1)
			return 0;
		return 20;
	}

	public Object getCellData(int row, int col) {

		switch (col) {
		case 0:
			return clientGroups.get(row2Id.get(String.valueOf(row)));
		default:
			return "";
		}
	}

	public String getMark(int row) {
		return clientGroups.get(row2Id.get(String.valueOf(row))).toString();
	}

	public String getGroupId(int row) {
		return row2Id.get(String.valueOf(row)).toString();
	}

	public TableCellAttributes getCellAttributes(int row, int col,
			boolean isSelected, TableCellAttributes ta) {
		ta = super.getCellAttributes(row, col, isSelected, ta);
		if (row != -1) {
			ta.clipCellData = true;
		}
		if (isSelected) {
			ta.foreground = Color.Black;
			ta.fillColor = TraderConstants.TBLMODEL_SELCOL;
		} else {
			if (row != -1) {
				String regionId = (String) row2Id.get(String.valueOf(row));
				if (Session.getInstance().containsGrClientId(regionId)
						&& row != -1) {
					ta.fillColor = Color.LightBlue;
				}
			}
			ta.foreground = Color.DarkGray;
			// ta.borderStyle = TableCellAttributes.BDR_NOBORDER;
		}
		return ta;
	}
}
